<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\ChenAppointment as Model;
use App\Models\Admin;
use App\Models\NewPrecut;
use App\Models\NewPrecutAssign;
use App\Models\NewPrecutCoverage;
use App\Models\NewPrecutPart;
use App\Models\NewPrecutFpart;
use App\Models\RollRestlen;
use App\Models\Warranty;
use App\Services\UserService;
use App\User;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpWord\PhpWord;

class StockController extends Controller
{
    public function index(Request $request)
    {
        $t = (int) $request->input('t');
        $admin_id = gui();
        $admin_type = gut();
        if ($admin_type == 1){
            $admin_id = 1;
        }

        // 套件
        if ($t == 2) {
            return $this->stock_precut($request, $admin_id);
        }
        if ($t == 3) {
            return $this->stock_precut_meal($request, $admin_id);
        }
        return $this->stock_rolls($request, $admin_id);

        $admin_type = gut();

        $page = [
            'page_name' => 'stock',
            'title' => '库存管理',
        ];
        return view('admin.stock.index-sd', [
            'page' => $page,
        ]);
        if ($admin_type == 2){
            return view('admin.stock.index-sd', [
                'page' => $page,
            ]);
        }
        return view('admin.stock.index-gly', [
            'page' => $page,
        ]);

    }


    public function show()
    {

    }


    /*
     * 用户库存*/
    public function dealer_stock(Request $request, $id)
    {
        // $id = DB::table('users')
        $t = (int) $request->input('t');
        $admin_type = gut();
        $admin_id = gui();
        if (!checkDealerBelongs($id)) {
            return abort(404);
        }
        // 套件
        if ($t == 2) {
            return $this->stock_precut($request, $id);
        }
        if ($t == 3) {
            return $this->stock_precut_meal($request, $id);
        }
        return $this->stock_rolls($request, $id);
    }
    /*
     * 卷库存*/
    public function stock_rolls(Request $request, $id)
    {
        $model = DB::table('roll_restlen as rr')
            ->leftJoin('roll_master as rm', function ($query) {
                $query->on('rm.roll_number', 'rr.roll_number')->whereNull('rm.deleted_at');
            })
            ->where('rr.user_id', $id)
            ->where('rr.restlen', '>', 0)
            ->whereNull('rr.deleted_at')
            ->select(
                [
                    'rr.roll_number',
                    'rr.restlen',
                    'rm.film_type_id',
                    'rm.id',
                ]
            );

        $model = filterByColumns($model, ['rr.roll_number']);
        $product_type_id = (int) $request->input('product_type_id');
        $film_type_id = (int) $request->input('film_type_id');
        if (!empty($film_type_id)) {
            $model = $model->where('rm.film_type_id', $film_type_id);
        } else if (!empty($product_type_id)) {
            $model = $model->leftJoin('film_type_master as ftmm', 'rm.film_type_id', 'ftmm.id')
                ->where('ftmm.product_type_id', $product_type_id);
        }

        $total = $model->sum('rr.restlen');
        $data = $model->paginate(50);

        $page = [
            'page_name' => 'stock',
            'title' => '库存管理',
        ];

        return view('admin.stock.stock-rolls', [
            'page' => $page,
            'data' => $data,
            'total' => $total,
            'user_type' => session('admin.user_type') ?: 0,
        ]);
    }
    /*
     * 套件库存*/
    public function stock_precut(Request $request, $id)
    {
        $model = DB::table('new_precut as p')
            ->leftJoin('new_precut_fpart as fp', function ($query) {
                $query->on('p.id', 'fp.precut_id')->whereNull('fp.deleted_at');
            })
            ->leftJoin('new_precut_coverage as fc', function ($query) {
                $query->on('p.id', 'fc.precut_id')->whereNull('fc.deleted_at');
            })
            ->whereNull('p.deleted_at')
            ->where('p.type',0);

        if (session('admin.user_type') != 4) {
            $model = $model->where(function ($query) use ($id) {
                $query->where('p.user_id', $id)
                    ->orWhere(function ($query) use ($id) {
                        $query->where('p.created_by', $id)
                            ->where(function ($query) {
                                $query->where('p.user_id', 0)
                                    ->orWhereNull('p.user_id');
                            });
                    });
            });
        } else {
            $userIdArr = (new UserService())->getBelongUserIdArr(session('admin.id'));

            $model = $model->where(function ($query) use ($userIdArr) {
                $query->whereIn('p.user_id', $userIdArr)
                    ->orWhere(function ($query) use ($userIdArr) {
                        $query->whereIn('p.created_by', $userIdArr)
                            ->where(function ($query) {
                                $query->where('p.user_id', 0)
                                    ->orWhereNull('p.user_id');
                            });
                    });
            });
        }

        $model = $model->groupBy('p.id');
        $model = filterByColumns($model, [
            'p.precut_id',
            'fc.roll_number'
        ]);

        $product_type_id = (int) $request->input('product_type_id');
        $film_type_id = (int) $request->input('film_type_id');
        if (!empty($film_type_id)) {
            $model = $model->where('fp.film_type_id', $film_type_id);
        } else if (!empty($product_type_id)) {
            $model = $model->leftJoin('film_type_master as ftmm', 'fp.film_type_id', 'ftmm.id')
                ->where('ftmm.product_type_id', $product_type_id);
        }

        $model = $model->select([
            'p.id',
            'p.precut_id',
            'fp.part_id',
            'fp.film_type_id',
            'fp.length',
            DB::raw("GROUP_CONCAT(fc.roll_number, '：', fc.length, ' cm' separator '\n') as roll_numbers "),
        ])->orderByDesc('p.id');
        $data = $model->paginate(50);
        $page = [
            'page_name' => 'stock',
            'title' => '库存管理',
        ];
        return view('admin.stock.stock-precut', [
            'page' => $page,
            'data' => $data,
        ]);
    }

    /*
     * 套餐库存*/
    public function stock_precut_meal(Request $request, $id)
    {
        $precutIds = [];
        $film_type_id = (int) $request->input('film_type_id');
        if (!empty($film_type_id)) {
            $precutIds = DB::table('new_precut_coverage')->where('film_type_id', $film_type_id)->pluck('precut_id')->toArray();
        }

        $model = DB::table('new_precut as p')
            ->leftJoin('new_precut_coverage as fc', function ($query) {
                $query->on('p.id', 'fc.precut_id')->whereNull('fc.deleted_at');
            })
            ->leftJoin('new_precut_fpart as fp', function ($query) {
                $query->on('p.id', 'fp.precut_id')->whereNull('fp.deleted_at')
                    ->on('fp.film_type_id', 'fc.film_type_id')->whereNull('fc.deleted_at');
            })
            ->leftJoin('film_type_master as fm', function ($query) {
                $query->on('fm.id', 'fc.film_type_id');
            })
            ->whereNull('p.deleted_at')
            ->where('p.type',1);

        if (session('admin.user_type') != 4) {
            $model = $model->where(function ($query) use ($id) {
                $query->where('p.user_id', $id)
                    ->orWhere(function ($query) use ($id) {
                        $query->where('p.created_by', $id)
                            ->where(function ($query) {
                                $query->where('p.user_id', 0)
                                    ->orWhereNull('p.user_id');
                            });
                    });
            });
        } else {
            $userIdArr = (new UserService())->getBelongUserIdArr(session('admin.id'));

            $model = $model->where(function ($query) use ($userIdArr) {
                $query->whereIn('p.user_id', $userIdArr)
                    ->orWhere(function ($query) use ($userIdArr) {
                        $query->whereIn('p.created_by', $userIdArr)
                            ->where(function ($query) {
                                $query->where('p.user_id', 0)
                                    ->orWhereNull('p.user_id');
                            });
                    });
            });
        }
        if (!empty($film_type_id)) {
            $model = $model->whereIn('p.id', $precutIds);
        }
        $model = $model->groupBy('p.id');
        $model = filterByColumns($model, [
            'p.precut_id',
            'fc.roll_number'
        ]);

        //$product_type_id = (int) $request->input('product_type_id');
//        else if (!empty($product_type_id)) {
//            $model = $model->leftJoin('film_type_master as ftmm', 'fp.film_type_id', 'ftmm.id')
//                ->where('ftmm.product_type_id', $product_type_id);
//        }

        $model = $model->select([
            'p.id',
            'p.precut_id',
            'fp.part_id',
            DB::raw('sum(fc.length) as length'),
            DB::raw("GROUP_CONCAT(fm.simplified_chiness_value, '： ', fc.roll_number, '： ', fc.length, ' cm' separator '\n') as roll_numbers "),
        ])->orderByDesc('p.id');
        $data = $model->paginate(50);
        $page = [
            'page_name' => 'stock',
            'title' => '库存管理',
        ];
        return view('admin.stock.stock-precut-meal', [
            'page' => $page,
            'data' => $data,
        ]);
    }
}

